package com.example.basic.web.service.newlyManage.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.web.mapper.device.DeviceMapper;
import com.example.basic.web.mapper.newly.SysFlowTaskMapper;
import com.example.basic.web.model.device.Device;
import com.example.basic.web.model.newly.SysFlowTask;
import com.example.basic.web.response.IndexTaskResponse;
import com.example.basic.web.service.newlyManage.ISysTaskManageService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysTaskManageServiceImpl implements ISysTaskManageService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SysFlowTaskMapper sysFlowTaskMapper;


    @Override
    public IPage<IndexTaskResponse> queryList(Page<SysFlowTask> queryPage, Integer status) {
        LambdaQueryWrapper<SysFlowTask> taskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(status)) {
            if (status == 0) {
                taskLambdaQueryWrapper.eq(SysFlowTask::getEndStatus, 0).or().eq( SysFlowTask::getStartStatus, 0);
            } else if (status == 1) {
                taskLambdaQueryWrapper.eq(SysFlowTask::getEndStatus, 1).eq(SysFlowTask::getStartStatus, 1);
            }
        }
        taskLambdaQueryWrapper.orderByDesc(SysFlowTask::getCreateAt);
        List<SysFlowTask> records = sysFlowTaskMapper.selectPage(queryPage, taskLambdaQueryWrapper).getRecords();

        List<IndexTaskResponse> indexTaskResponses = records.stream().map(record -> {
            IndexTaskResponse indexTaskResponse = new IndexTaskResponse();
            indexTaskResponse.setId(record.getId());
            indexTaskResponse.setNowStage(record.getNowStage());
            Device startDevice = deviceMapper.selectById(record.getStartPositionId());
            indexTaskResponse.setStartPositionName(startDevice.getDeviceName());
            Device endDevice = deviceMapper.selectById(record.getEndPositionId());
            indexTaskResponse.setEndPositionName(endDevice.getDeviceName());
            indexTaskResponse.setCreateAt(record.getCreateAt());
            if (record.getStartStatus()==0 || record.getEndStatus()==0){
                indexTaskResponse.setStatus(0);
            }else {
                indexTaskResponse.setStatus(1);
            }
            return indexTaskResponse;
        }).collect(Collectors.toList());

        Page<IndexTaskResponse> indexTaskResponsePage = new Page<>(queryPage.getCurrent(), queryPage.getSize(), queryPage.getTotal());
        indexTaskResponsePage.setRecords(indexTaskResponses);
        return indexTaskResponsePage;
    }
}
